package com.lhd.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.lhd.entity.po.UserCourse;
import com.lhd.entity.query.UserCourseQuery;
import com.lhd.mappers.UserCourseMapper;
import org.springframework.stereotype.Service;

import com.lhd.entity.enums.PageSize;
import com.lhd.entity.query.CourseQuery;
import com.lhd.entity.po.Course;
import com.lhd.entity.vo.PaginationResultVO;
import com.lhd.entity.query.SimplePage;
import com.lhd.mappers.CourseMapper;
import com.lhd.service.CourseService;
import com.lhd.utils.StringTools;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: liuhd
 * @Date: 2025/03/16
 * @Description: Course业务接口实现类
 */
@Service
public class CourseServiceImpl implements CourseService { 
	@Resource
	private CourseMapper<Course, CourseQuery> courseMapper;
	@Resource
	private UserCourseMapper<UserCourse, UserCourseQuery> userCourseMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<Course> findListByQuery(CourseQuery query){
		return courseMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	@Override
	public Long findCountByQuery(CourseQuery query){
		return courseMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	@Override
	public PaginationResultVO<Course> findListByPage(CourseQuery query){
		// 查出查询的结果数量
		long count = findCountByQuery(query);
		// 如果没给pageSize 默认是15
		long pageSize = query.getPageSize() == null ? PageSize.SIZE15.getSize() : query.getPageSize();
		// 构建SimplePage 可以确定limit的index与offset以及总页数 以及真正的pageNo
		SimplePage page = new SimplePage(query.getPageNo(), count, pageSize);
		// 设置到query中 xml中会使用query.simplePage.start与query.simplePage.end
		query.setSimplePage(page);
		// 用query查询 此时是分页条件查询
		List<Course> list = this.findListByQuery(query);
		// 将结果塞到PaginationResultVO中
		PaginationResultVO<Course> result = new PaginationResultVO<>(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	@Transactional
	public Long add(Course bean){

		Long res = courseMapper.insert(bean);

		UserCourse userCourse = new UserCourse();
		userCourse.setUid(bean.getUid());
		// 主键回显
		userCourse.setCid(bean.getId());
		userCourseMapper.insert(userCourse);
		return res;
	}


	/**
	 * 新增或更新
	 */
	@Override
	public Long addOrUpdate(Course bean){
		return courseMapper.insertOrUpdate(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Long addBatch(List<Course> listBean){
		if (listBean == null || listBean.isEmpty()) {
			return 0L;
		}
		return courseMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或更新
	 */
	@Override
	public Long addOrUpdateBatch(List<Course> listBean){
		if (listBean == null || listBean.isEmpty()) {
			return 0L;
		}
		return courseMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Long updateByQuery(Course bean, CourseQuery query){
        return courseMapper.updateByQuery(bean, query);
	}

	/**
	 * 多条件删除
	 */
	@Override
	@Transactional
	public Long deleteByQuery(CourseQuery query){
		return courseMapper.deleteByQuery(query);
	}

	/**
	 * 根据PRIMARY查询
	 */
	@Override
	public Course getCourseById(Integer id){
		return courseMapper.selectById(id);
	}

	/**
	 * 根据PRIMARY删除
	 */
	@Override
	public Long deleteCourseById(Integer id){
		Long res = courseMapper.deleteById(id);
		UserCourseQuery userCourseQuery = new UserCourseQuery();
		userCourseQuery.setCid(id);
		userCourseMapper.deleteByQuery(userCourseQuery);
		return res;
	}

	/**
	 * 根据PRIMARY更新
	 */
	@Override
	public Long updateCourseById(Course bean, Integer id){
		return courseMapper.updateById(bean, id);
	}

}